package com.chen.dao.impl;

import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.chen.dao.UserDao;
import com.chen.pojo.User;

@Repository
public class UserDaoImpl implements UserDao{
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	public List<User> login(User user) {
		System.out.println("-------------------");
		String HQL = "from User where username = ? and password = ?";
		List<User> list = (List<User>) hibernateTemplate.find(HQL, user.getUsername(),user.getPassword());
		return list;
	}

	@Override
	public void register(User user) {
		int i = (int) hibernateTemplate.save(user);
		if(i > 0){
			System.out.println("注册成功");
		}else{
			System.out.println("注册失败");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll() {
		return (List<User>) hibernateTemplate.find("from User");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> edit(int id) {
		String HQL = "from User where id = ?";
		return (List<User>) hibernateTemplate.find(HQL,id);
	}

	@Override
	public void update(User user) {
		hibernateTemplate.update(user);
	}	
}